#pragma once
#include <list>
#include <thread>
#include "ThreadManager.h"
extern "C"
{
#include <libavutil/avutil.h>
}

struct AVCodecContext;
struct AVFrame;
struct AVStream;
struct AVCodecParameters;
class Demux;
class PacketQueue;
class FrameQueue;
class Session;
class Decoder
{
public:
	Decoder(Session* session, AVStream* stream, int maxFrameSize);
	~Decoder();

	void Start(AVCodecParameters* params);

private:

	bool Open(AVCodecParameters* params);

	void StartProcess(AVCodecParameters* params);

	void Close();


	//int GetVideoFrame(AVFrame* &frame);
	int DecodeFrame(AVFrame* &frame);

public:
	PacketQueue* pktQueue = NULL;
	FrameQueue* frameQueue = NULL;
	bool endOfFile = false;
	//int64_t last_pts = 0;
	MUTEX frameMux;
	double duration = 0;
private:

	AVCodecContext* ctx = NULL;
	
	Session* session = NULL;

	AVMediaType decodecType;

	MUTEX processMux;
	
	

	std::thread process_t;

	int pkt_serial = -1;
};